package yl.hs.bmipfull.service.chart.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.daos.salemanage.IContractControlInfoDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractDao;
import yl.hs.bmipfull.daos.salemanage.ISaleOrderDao;
import yl.hs.bmipfull.service.chart.ISaleChartService;
import yl.hs.bmipfull.service.salemanage.impl.SaleBaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.datastate.ContractControlType;

import java.time.Period;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote
 * @since 2022-03-16 8:58 创建
 */
@Service
public class SaleChartServiceImpl extends SaleBaseService implements ISaleChartService {

    @Autowired
    ISaleOrderDao saleOrderDao;

    @Autowired
    ISaleContractDao contractDao;

    @Autowired
    IContractControlInfoDao controlInfoDao;

    @Autowired
    IProductShipOrderDao orderDao;

    @Override
    public Integer getUntreatedOrder(int type) {
        Integer result=0;
        if (type==1){
            var user= HsApp.getCurrent();
            result=saleOrderDao.getUntreatedOrder(user.getUserSN());
        }else{
            result=saleOrderDao.getUntreatedOrder(null);
        }
        return result;
    }

    @Override
    public Integer getEarlyWarningContract() {
        var user = HsApp.getCurrent();
        var tags = getTagSNs();
        var htList = contractDao.querySNListByTags(tags);
        var result = 0;
        if(htList.size()>0) {
            var controlList = controlInfoDao.queryListByContractSN(htList);
            for (var htSN : htList){
                var ctrlList = controlList.stream().filter(t->t.getContractSN().equals(htSN)).toList();
                if(ctrlList.size()>0){
                    for (var ctrlObj : ctrlList){
                        boolean check = false;
                        if(ctrlObj.getControlType()== ContractControlType.Vol.getCode()) {
                            check = checkControlShipQuantity(htSN, ctrlObj.getStartTime(), ctrlObj.getAlertValue());
                        }
                        else if(ctrlObj.getControlType() == ContractControlType.Money.getCode())
                        {
                            check = checkControlShipAmount(htSN,ctrlObj.getStartTime(),ctrlObj.getAlertValue());
                        }
                        else if( ctrlObj.getControlType() == ContractControlType.Day.getCode())
                        {
                            if(ctrlObj.getStartTime().compareTo(new Date())<=0){
                                long now = (new Date()).getTime();
                                long start = ctrlObj.getStartTime().getTime();
                                var days = (now-start) / ((1000*60*60*24)*1.0f);
                                check = days>ctrlObj.getAlertValue();
                            }
                        }

                       if(check){
                           result ++;
                           break;
                       }
                    }
                }
            }
        }
        return result;
    }

    /**
     *
     */
    public boolean checkControlShipQuantity(String htSN, Date startTime, float alertValue){
        var quantity = orderDao.getControlShipQuantityCount(htSN,startTime);
        return quantity.floatValue()>=alertValue;
    }

    /**
     *
     */
    public boolean checkControlShipAmount(String htSN, Date startTime, float alertValue){
        var quantity = orderDao.getControlShipAmountCount(htSN,startTime);
        return quantity.floatValue()>=alertValue;
    }
}
